#!/bin/sh
#
# REMOTE-DEVICE-REMOVE(7)
# =======================
#
# NAME
# ----
# remote-device-remove - script for cleaning up the Re-Mote device hierarchy
#
# SYNOPSIS
# --------
# /lib/udev/remote-device-remove
#
# DESCRIPTION
# -----------
# This script is designed to be called via a UDEV rule's RUN key. It
# cleans up after removed motes and notifies the motehost about the
# changed state of the device hierarchy.
#
# ENVIRONMENT VARIABLES
# ---------------------
# ACTION::
#
#	This environment variable is set by UDEV. Unless the value is
#	"remove" the script will exit.
#
# FILES
# -----
# /dev/remote/MOTEMAC/tty/control::
# /dev/remote/MOTEMAC/tty/data::
#
#	Symlink to the mote's data and control TTY devices. These are
#	checked for existence in order to determine whether the mote
#	directory and files created by remote-device-add(7) should be
#	cleanup.
#
# /var/log/remote-udev.log::
#
#	Log file for the Re-Mote UDEV scripts.
#
# /var/run/motehost.events::
#
#	Path to the pipe where the mote host can be notified about the
#	remove event.
#
# EXAMPLE
# -------
# This script should usually be run using a UDEV rule similar to the
# following:
#
#	SUBSYSTEM=="tty", ACTION=="remove", RUN+="/lib/udev/remote-device-remove"
#
# COPYRIGHT
# ---------
# Copyright (c) 2007 Jonas Fonseca <fonseca@diku.dk>
# Copyright (c) 2008 Rostislav Spinar <rostislav.spinar@cit.ie>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# SEE ALSO
# --------
# remote-device-add(7)
#
########################################################################
# Configuration section
########################################################################

# The path to the device hierarchy
DEVROOT="/dev/remote"

# File to log messages to; leave empty to disable logging
LOGFILE="/var/log/remote-udev.log"

# Path to the MCH event pipe
MCHPIPE="/var/run/motehost.events"

# Path to the program that should be used for signaling MCH
MCHNOTIFY="/sbin/remote-mch-notify"

########################################################################
# The script
########################################################################

info ()
{
	test -n "$LOGFILE" && echo "[$(date)] $ACTION: $*" >> "$LOGFILE"
}

die ()
{
	info "$*"
	exit 0
}

test "$ACTION" = "remove" || die "Requested action not remove ($ACTION)"
test -d "$DEVROOT" || die "Device root is not a directory ($DEVROOT)"

tmpdir="$DEVROOT/.tmp"

# Use the tmpdir as a semaphore when multiple motes are removed
test -d "$tmpdir" && return
mkdir -p "$tmpdir" || die "Failed to create temp dir '$tmpdir'"

# Remove devices where no tty link exists.
ls "$DEVROOT/" | while read motedir;
do
	dir="$DEVROOT/$motedir"
	test -e "$dir/tty/data" -a -e "$dir/tty/control" && continue;
	info "Removing mote '$motedir'"
	# Cannot die here since we need to remove the temp dir
	mv "$dir" "$tmpdir" || info "Failed to move '$dir' to '$tmpdir'"
done

# The temp directory is empty?
rmdir "$tmpdir" && return

# Fatal but the mote host should still be notified
rm -rf "$tmpdir" || info "Failed to remove directory '$tmpdir'"

# Notify mote host
test -p "$MCHPIPE" || die "Event pipe does not exist at '$MCHPIPE'"
"$MCHNOTIFY" "$MCHPIPE" || die "Event program returned error"
